/**
 * Created by zy on 2016/6/23.
 * 新建 - 缺陷维修管理 - 管体缺陷
 */


;(function ($, window, document, undefined) {

    // 维修详情模板
    window.repairTemplate = [
        {name: 'defectId', title: '选中'},
        {name: 'pipeName', title: '管道名称：'},
        {name: 'featureSubType', title: '缺陷类型：'},
        {name: 'mileage', title: '绝对距离(m)：'},
        {
            name: 'length', title: '长/宽/深(mm)：', render: function (d, r) {
            return r.length + '/' + r.width + '/' + r.depth
        }
        },
        {name: 'clock', title: '时钟位置：'},
        {name: 'erf', title: 'ERF：'},
        {name: 'repairSuggestion', title: '维修建议：'},
        {
            name: 'planRepairTime', title: '最迟维修时间：', render: function (d) {
            return Dyy.getDate(d);
        }
        },
        {name: 'planRepairType', title: '建议维修方法：'},
        {name: 'defectId', title: '详情'}
    ];

    window.repairTemplate2 = [
        {name: 'customNumber', title: '缺陷编号：'},
        {name: 'pipeName', title: '管道名称：'},
        {name: 'featureSubType', title: '缺陷类型：'},
        {name: 'mileage', title: '绝对距离(m)：'},
        {
            name: 'length', title: '长/宽/深(mm)：', render: function (d, r) {
            return r.length + '/' + r.width + '/' + r.depth
        }
        },
        {name: 'clock', title: '时钟位置：'},
        {name: 'erf', title: 'ERF：'},
        {name: 'repairSuggestion', title: '维修建议：'},
        {
            name: 'planRepairTime', title: '最迟维修时间：', render: function (d) {
            return Dyy.getDate(d);
        }
        },
        {name: 'planRepairType', title: '建议维修方法：'}
    ];

    // 工作量模板
    window.workTemplate = [
        {name: 'coatRepair', title: '防腐层修复'},
        {name: 'compositesRepair', title: '复合材料补强修复'},
        {name: 'epoxySleeveRepair', title: '钢质环氧套筒补强修复'},
        {name: 'atypeSleeveRepair', title: 'A型套筒补强修复'},
        {name: 'btypeSleeveRepair', title: 'B型套筒补强修复'},
        {name: 'pipeReplacement', title: '换管'},
        {name: 'excavationVerification', title: '开挖验证'},
        {name: 'otherRepairWf', title: '其它修复'},
        {name: 'total', title: '合计'}
    ];

    // 初始数据项
    window.initData = function () {
        var data = {};
        data.id = function(){
            var v = $('#JQ_task_id').val();
            if(v){
                $('#JQ_action_step').text('编辑');
                $('#JQ_action_step2').text('编辑');
                return v;
            }
            return '';
        }();
        data.defectInfoId = [];
        data.status = '';
        return data;
    }();

    /* ---------------- 新建表单 start ---------------- */
    // 文件文本
    var $export_file = $('#JQ_export_file');
    // 文件
    var $input_file = $('#JQ_input_file');
    // 项目名称
    var $project_name = $('#JQ_project_name');
    // 施工单位
    var $company = $('#JQ_company');
    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        mileType: 'float',
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment
        }
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        mileType: 'float',
        param: {
            'pipeLineId': $pipeline,
            'pipeSegmentId': $segment,
            'beginSignId': $begin
        }
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 开工时间
    var $begin_date = $('#JQ_begin_date');
    // 峻工时间
    var $end_date = $('#JQ_end_date');
    // 表单
    var $form_create = $('#JQ_form_create');
    // 下一步按钮
    var $go_next = $('#JQ_go_next');
    // 工作量表单
    var $work_form = $('#JQ_work_form');
    /* ---------------- 新建表单 end ---------------- */


    /* ---------------- 查询表单 start ---------------- */
    // 缺陷类型
    var $defect_type = $('#JQ_defect_type').select3({
        search: false
    });
    // 填充缺陷类型
    DyyBase.ajax(defectApi.url_defect_class(), {}, function (dataset) {
        var json = DyyBase.getSelect(dataset);
        $defect_type.add(json);
    });
    // 建议最迟维修时间 - 1
    var $repair_date_begin = $('#JQ_repair_date_begin');
    // 建议最迟维修时间 - 2
    var $repair_date_end = $('#JQ_repair_date_end');

    /* ---------------- 查询表单 end ---------------- */


    /* ---------------- 新建操作 start ---------------- */
    // 选择文件
    $input_file.on('change', function () {
        if(window.initData.id){
            window.initData.status = 'update';
            $go_next.attr('data-action','update');
        }else{
            $go_next.attr('data-action','create');
        }
        $export_file.val(this.value);
    });
    // 下一步
    $go_next.click(function () {
        var $this = $(this);
        var action = $this.attr('data-action');
        var param = filter.getForm();
        var $hidden_div = $('#JQ_hidden_div').empty();
        if (param.mode) {

            window.initData.name = param.name;
            window.initData.assemblyCompany = param.assemblyCompany;

            window.initData.pipeLineId = param.pipeLineId;
            window.initData.pipeLineName = param.pipeLineName;

            window.initData.fromPointId = param.fromPointId;
            window.initData.fromPointName = param.fromPointName;
            window.initData.fromPointMile = param.fromPointMile;

            window.initData.toPointId = param.toPointId;
            window.initData.toPointName = param.toPointName;
            window.initData.toPointMile = param.toPointMile;

            window.initData.planStartDate = param.planStartDate;
            window.initData.planCompletionDate = param.planCompletionDate;

            if (action === 'create' || action === 'update') {
                if(window.initData.id && window.initData.status === 'edit'){
                    loadEdit();
                    // 显示隐藏
                    $('#JQ_form_div').hide();
                    $('#JQ_list_div').show();
                }else{
                    for (var k in param) {
                        var $input = $('<input type="hidden">');
                        $input.prop('name', k).val(param[k]);
                        $hidden_div.append($input);
                    }
                    $form_create.submit();
                }
            } else if (action === 'edit') {
                // 显示隐藏
                $('#JQ_form_div').hide();
                $('#JQ_list_div').show();
            }
        }
    });
    // 返回列表
    $('#JQ_go_list').click(function () {
        window.location.href = DyyBase.root_path + '/pbf/RepairTask/init.html';
    });
    // 返回上一步
    $('#JQ_goback_task').click(function () {
        // 显示隐藏
        $('#JQ_form_div').show();
        $('#JQ_list_div').hide();
        $go_next.attr('data-action','update');
    });
    /* ---------------- 新建操作 end ---------------- */


    /* ---------------- 隐藏项 start ---------------- */
    // 已导入缺陷点列表
    var $selected_list = $('#JQ_selected_list').checkbox3();
    // 所有缺陷点列表
    var $gather_list = $('#JQ_gather_list').checkbox3(), DTGATHER;
    // 管道
    window.$pipe_gather = $('#JQ_pipe_gather').select3();
    // 起点位置
    window.$begin_gather = $('#JQ_begin_gather').select3();
    // 终点位置
    window.$end_gather = $('#JQ_end_gather').select3();
    /* ---------------- 隐藏项 end ---------------- */


    /* ---------------- 编辑 start ---------------- */
    if(window.initData.id){
        loadEdit();
    }
    function loadEdit(){
        DyyBase.ajax(defectApi.url_repair_details(),{repairTaskId:initData.id},function(dataset){
            if(dataset.bool){

                window.initData.status = 'edit';

                // 填充表单
                var repairTask = dataset.result.repairTask;
                $project_name.val(repairTask.name);
                $company.val(repairTask.assemblyCompany);
                $begin_date.val(repairTask.planStartDate);
                $end_date.val(repairTask.planCompletionDate);
                $pipeline.fit(repairTask.pipeLineId).disable();
                $segment.disable();
                $begin.asset([{id:repairTask.fromPointId,text:repairTask.fromPointName,mile:repairTask.fromPointMile}]).disable();
                $end.asset([{id:repairTask.toPointId,text:repairTask.toPointName,mile:repairTask.endPointMile}]).disable();
                $export_file.val('您还可以修改上传文件');

                // 填充列表
                var defectInfoList = dataset.result.defectInfoList.list;
                window.fillList(DyyBase.clearNull(defectInfoList), undefined, 'update');

                // 填充工作量
                var repairTaskWorkload = dataset.result.repairTaskWorkload;
                window.fillWork(DyyBase.clearNaN(repairTaskWorkload));

                // 填充查询条件
                window.$pipe_gather.asset([{id:repairTask.pipeLineId,text:repairTask.pipeLineName}]).disable();
                window.$begin_gather.asset([{id:repairTask.fromPointId,text:repairTask.fromPointName,mile:repairTask.fromPointMile}]).disable();
                window.$end_gather.asset([{id:repairTask.toPointId,text:repairTask.toPointName,mile:repairTask.endPointMile}]).disable();

                // 按钮状态
                $go_next.attr('data-action', 'edit');
            }
        });
    }
    /* ---------------- 编辑 end ---------------- */



    // 查询对象
    var filter = {
        // 获得表单
        getForm: function () {
            var data = {};
            data.mode = true;
            // 文件
            data.export_file = function () {
                var v = $.trim($export_file.val());
                if (v) {
                    return v;
                } else {
                    $export_file.TIP({content: '请选择导入文件!'});
                    data.mode = false;
                }
                return '';
            }();
            // 任务ID
            data.id = function(){
                return window.initData.id;
            }();
            // 项目名称
            data.name = function () {
                var v = $.trim($project_name.val());
                if (v) {
                    return v;
                } else {
                    $project_name.TIP({content: '项目名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 施工单位
            data.assemblyCompany = function () {
                var v = $.trim($company.val());
                if (v) {
                    return v;
                } else {
                    $company.TIP({content: '施工单位不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 管道ID
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                } else {
                    $pipeline.element.TIP({content: '管道名称不能为空!'});
                    data.mode = false;
                }
                return '';
            }();
            // 管道名称
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            // 起点位置ID
            data.fromPointId = function () {
                var v = $begin.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            // 起点位置NAME
            data.fromPointName = function () {
                var v = $begin.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            // 起点位置距离
            data.fromPointMile = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            // 终点位置ID
            data.toPointId = function () {
                var v = $end.val();
                if (v) {
                    return v;
                }
                return '';
            }();

            // 终点位置NAME
            data.toPointName = function () {
                var v = $end.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            // 终点位置距离
            data.toPointMile = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            // 开工时间
            data.planStartDate = function () {
                var v = $begin_date.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            // 开工时间
            data.planCompletionDate = function () {
                var v1 = $begin_date.val();
                var v2 = $end_date.val();
                if (v2) {
                    if (v1) {
                        if (!(Dyy.checkDateLate(v1, v2))) {
                            $end_date.TIP({content: '峻工时间应当晚于开工时间!'});
                            data.mode = false;
                        }
                    }
                    return v2;
                }
                return '';
            }();
            // 缺陷ID
            data.defectInfoId = function () {
                return window.initData.defectInfoId;
            }();
            return data;
        },
        // 查询参数
        getSearch: function () {
            var data = {};
            data.instTaskId = function () {
                return window.initData.id ? window.initData.id : '';
            }();
            data.pipeId = function () {
                return window.initData.pipeLineId ? window.initData.pipeLineId : '';
            }();
            data.beginMileage = function () {
                return DyyBase.notEmpty(window.initData.fromPointMile) ? window.initData.fromPointMile : '';
            }();
            data.endMileage = function () {
                return DyyBase.notEmpty(window.initData.toPointMile) ? window.initData.toPointMile : '';
            }();
            data.featureType = function () {
                var v = $defect_type.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.erf = function () {
                if ($('#JQ_erf').hasClass('active')) {
                    return '1.0';
                }
                return '';
            }();
            data.excludeDeIds = function () {
                return window.initData.defectInfoId ? window.initData.defectInfoId : '';
            }();
            data.latestBeginTime = function () {
                var v = $repair_date_begin.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.latestEndTime = function () {
                var v = $repair_date_end.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            return data;
        },
        // 获得集合ids
        getGatherIds: function () {
            var arr = [];
            var $chks = $gather_list.find('i.zy-checkbox');
            if ($chks.length > 0) {
                $chks.each(function () {
                    var $this = $(this);
                    if ($this.hasClass('active')) {
                        arr.push($this.attr('data-id'));
                    }
                });
            }
            return arr;
        },
        // 获得已选择ids
        getSelectedIds: function (flag) {
            var arr = [];
            var $chks = $selected_list.find('i.zy-checkbox');
            if ($chks.length > 0) {
                $chks.each(function () {
                    var $this = $(this);
                    if (flag) {
                        if ($this.hasClass('active')) {
                            arr.push($this.attr('data-id'));
                        }
                    } else {
                        arr.push($this.attr('data-id'));
                    }
                });
            }
            return arr;
        },
        // 获取保存&发布
        getSave: function () {
            var param = filter.getForm();
            var $input = $('#JQ_work_load').find('input[type="text"]');
            $input.each(function () {
                var $this = $(this);
                var name = $this.prop('name'), value = $this.val();
                param[name] = value;
            });
            return param;
        }
    };

    // 加载所有缺陷点列表
    function loadGather(flag) {
        if (flag) {
            if (DTGATHER) {
                DTGATHER.draw();
            }
        } else {
            if (!DTGATHER) {
                DTGATHER = $gather_list.DataTable({
                    'language': {
                        'zeroRecords': DyyBase.defect_empty()
                    },
                    'info': false,
                    'autoWidth': false,
                    'serverSide': true,
                    'ordering': false,
                    'processing': false,
                    'searching': false,
                    'lengthChange': false,
                    'paging': false,
                    'pageLength': 1000,
                    'ajax': function (data, callback) {
                        var param = DyyBase.convertToArr(filter.getSearch());
                        DyyBase.ajax(defectApi.url_advise_list(), param, function (dataset) {
                            var returnData = {};
                            returnData.draw = data.draw;
                            returnData.recordsTotal = 1000;
                            returnData.recordsFiltered = 1000;
                            returnData.data = DyyBase.clearNull(dataset.result);
                            callback(returnData);
                        });
                    },
                    'drawCallback': function () {
                        $gather_list.find('i.zy-checkbox-all').trigger('clear');
                    },
                    'columns': [
                        {
                            data: 'defectId', render: function (data) {
                            return '<i class="zy-checkbox" data-id="' + data + '" title="选中"></i>';
                        }
                        },
                        {
                            data: 'pipeName', render: function (data) {
                            return '<span class="l" title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'featureSubType', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'mileage', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'length', render: function (data, type, row) {
                            return '<span title="' + data + '">' + data + '/' + row.width + '/' + row.depth + '</span>';
                        }
                        },
                        {
                            data: 'clock', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'erf', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'repairSuggestion', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'planRepairTime', render: function (data) {
                            return '<span title="' + Dyy.getDate(data) + '">' + Dyy.getDate(data) + '</span>';
                        }
                        },
                        {
                            data: 'planRepairType', render: function (data) {
                            return '<span title="' + data + '">' + data + '</span>';
                        }
                        },
                        {
                            data: 'defectId', render: function (data) {
                            return '<i data-id="' + data + '" class="zy-details-icon" title="展开详情"></i>';
                        }
                        }
                    ]
                });
            }
        }
    }

    // 添加更多缺陷
    $('#JQ_add_more').click(function () {
        var $this = $(this);
        $this.toggleClass('active');
        if ($this.hasClass('active')) {
            $('#JQ_more_list').fadeIn();
            loadGather();
        } else {
            $('#JQ_more_list').hide();
        }
    });

    // 添加缺陷点
    $('#JQ_add').click(function () {
        var $this = $(this);
        var ids = filter.getGatherIds();
        if (ids.length > 0) {
            var list = [];
            var $tr = $gather_list.find('tr');
            $tr.each(function () {
                var $this_tr = $(this);
                var $chk = $this_tr.find('i.zy-checkbox');
                if ($chk.hasClass('active')) {
                    list.push(DTGATHER.row($this_tr).data());
                }
            });
            // 填充列表
            window.fillList(list, true, 'edit');
            // 排除
            loadGather(true);
        } else {
            $this.MSG({
                content: '请选择要添加的缺陷点!'
            });
        }
    });

    // 删除缺陷点
    $('#JQ_delete').click(function () {
        var $this = $(this);
        var arr = filter.getSelectedIds(true);
        if (arr.length > 0) {
            $go_next.attr('data-action', 'edit');
            // 删除注入
            window.initData.defectInfoId = DyyBase.deleteArray(window.initData.defectInfoId, arr);
            var $chk = $selected_list.find('i.zy-checkbox');
            $chk.each(function () {
                var $this_i = $(this);
                if ($this_i.hasClass('active')) {
                    var id = $this_i.attr('data-id');
                    $this_i.parent('td').parent('tr').remove();
                    $selected_list.find('tr[data-id="'+id+'"]').remove();
                }
            });
            var $selected_tbody = $('#JQ_selected_tbody');
            if (!($selected_tbody.html())) {
                var empty = '<tr><td colspan="11">' + DyyBase.defect_empty() + '</td></tr>';
                $selected_tbody.attr('data-status', 'empty').html(empty);
            }
            $selected_list.find('i.zy-checkbox-all').trigger('clear');
            // 填充工作量
            if(window.initData.defectInfoId.length > 0){
                DyyBase.ajax(defectApi.url_advise_work(), DyyBase.convertToArr({defectInfoId: window.initData.defectInfoId}), function (dataset) {
                    if(dataset.bool){
                        var data = DyyBase.clearNaN(dataset.result);
                        window.fillWork(data);
                    }
                });
            }else{
                fillWork({});
            }
            // 排除
            loadGather(true);
        } else {
            $this.MSG({
                content: '请选择要删除的缺陷点!'
            });
        }
    });

    // 查询
    $('#JQ_search').click(function () {
        loadGather(true);
    });

    // 保存
    $('#JQ_save_task').click(function () {
        var $this = $(this);
        var param = filter.getSave();
        var bool = $work_form.verify();
        if(param.mode && bool){
            param.dataStated = '01';
            DyyBase.ajax(defectApi.url_repair_step2(), DyyBase.convertToArr(param), function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '保存维修项目成功!',
                        yesBtnAct: DyyBase.goToPage,
                        closeBtnAct: DyyBase.goToPage,
                        goToUrl: DyyBase.root_path + '/pbf/RepairTask/init.html'
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        }
    });

    // 发布
    $('#JQ_release_task').click(function () {
        var $this = $(this);
        var param = filter.getSave();
        var bool = $work_form.verify();
        if(param.mode && bool){
            param.dataStated = '20';
            DyyBase.ajax(defectApi.url_repair_step2(), DyyBase.convertToArr(param), function (dataset) {
                if (dataset.bool) {
                    $this.MSG({
                        type: 'success',
                        content: '发布维修项目成功!',
                        yesBtnAct: DyyBase.goToPage,
                        closeBtnAct: DyyBase.goToPage,
                        goToUrl: DyyBase.root_path + '/pbf/RepairTask/init.html'
                    });
                } else {
                    $this.MSG({
                        type: 'error',
                        content: dataset.result
                    });
                }
            });
        }
    });

    // 详情 - 有两种情况
    $gather_list.on('click', 'i.zy-details-icon', function () {
        DyyBase.showDetails($(this), DTGATHER, $gather_list, window.repairTemplate2);
    });

    $selected_list.on('click', 'i.zy-details-icon', function () {
        var $this = $(this);
        var $tr = $this.parent('td').parent('tr');
        var data = $tr.data('store');
        DyyBase.showDetails($(this), {}, $selected_list, window.repairTemplate2, data);
    });
})(jQuery, window, document);

// 回调
window.getResult = function (dataset,flag) {
    if(flag === undefined){
        if (dataset.bool) {
            // 显示隐藏
            $('#JQ_form_div').hide();
            $('#JQ_list_div').show();

            var list = [];
            if(dataset.result.hasOwnProperty('defectInfoList')){
                if(dataset.result.defectInfoList.hasOwnProperty('list')){
                    list = dataset.result.defectInfoList.list;
                }else{
                    list = dataset.result.defectInfoList;
                }
            }
            fillList(DyyBase.clearNull(list));
            var work = {};
            if(dataset.result.hasOwnProperty('repairTaskWorkload')){
                work = dataset.result.repairTaskWorkload;
            }
            fillWork(DyyBase.clearNaN(work));

            // 填充控件值
            window.$pipe_gather.asset([
                {
                    id: window.initData.pipeLineId,
                    text: window.initData.pipeLineName
                }
            ]).disable();

            window.$begin_gather.asset([
                {
                    id: window.initData.fromPointId,
                    text: window.initData.fromPointName,
                    mile: window.initData.fromPointMile
                }
            ]).disable();

            window.$end_gather.asset([
                {
                    id: window.initData.toPointId,
                    text: window.initData.toPointName,
                    mile: window.initData.fromPointMile
                }
            ]).disable();
        } else {
            $(this).MSG({
                type: 'error',
                content: dataset.result,
                yesBtnAct: function(){
                    if(dataset.message == 'sessionStatusFalse'){
                        DyyBase.goLogin();
                    }
                },
                closeBtnAct: function(){
                    if(dataset.message == 'sessionStatusFalse'){
                        DyyBase.goLogin();
                    }
                }
            });
        }
    }

    // 填充已导入作业点列表
    function fillList(data, tag, flag) {
        // 构造列表
        var $selected_tbody = $('#JQ_selected_tbody');
        var $go_next = $('#JQ_go_next');
        var action = $go_next.attr('data-action');
        var status = $selected_tbody.attr('data-status');
        if(action === 'update' || status === 'empty'){
            $go_next.attr('data-action', 'edit');
            $selected_tbody.empty();
        }
        if(flag){
            $selected_tbody.attr('data-status', flag);
            $go_next.attr('data-action', flag);
        }

        if(data.length){
            for (var i = 0, len = data.length; i < len; i++) {
                var $tr = $('<tr></tr>');
                var td = '';
                for (var j = 0, lens = window.repairTemplate.length; j < lens; j++) {
                    // 注入缺陷
                    if (j === 0) {
                        window.initData.defectInfoId.push(data[i]['defectId']);
                    }
                    // 构造列
                    if (data[i].hasOwnProperty(window.repairTemplate[j]['name'])) {
                        var title = window.repairTemplate[j]['title'];
                        var name = data[i][window.repairTemplate[j]['name']];

                        if (window.repairTemplate[j].hasOwnProperty('render')) {
                            name = window.repairTemplate[j]['render'](name, data[i]);
                        }

                        if (title === '选中') {
                            td += '<td><i title="选中" class="zy-checkbox" data-id="' + name + '"></i></td>';
                        } else if (title === '详情') {
                            td += '<td><i data-id="' + name + '" class="zy-details-icon" title="展开详情"></i></td>';
                        } else {
                            td += '<td><span title="' + name + '">' + name + '</span></td>';
                        }
                    }
                }
                $tr.append(td).data('store', data[i]);
                $selected_tbody.append($tr);
            }
            $selected_tbody.attr('data-status', 'edit');
        }

        // 重新填充工作量
        if(tag){
            DyyBase.ajax(defectApi.url_advise_work(), DyyBase.convertToArr({defectInfoId: window.initData.defectInfoId}), function (dataset) {
                var param = DyyBase.clearNaN(dataset.result);
                fillWork(param);
            });
        }
    }

    // 填充工作量
    function fillWork(data) {
        if(!($.isEmptyObject(data))){
            var $work_load = $('#JQ_work_load').empty().append('<div>主要工作量：</div>');
            var count = 0;
            for (var i = 0, len = window.workTemplate.length; i < len; i++) {
                var label = '';
                var title = window.workTemplate[i]['title'];
                var name = data[window.workTemplate[i]['name']];
                if (data.hasOwnProperty(window.workTemplate[i]['name'])) {
                    label += '<label>' + title + '<span><input type="text" class="zy-s-input zy-verify" data-verify="{type:\'number\',range:[0,10000],title:\''+ window.workTemplate[i]['title'] +'\'}" name="' + window.workTemplate[i]['name'] + '" value="' + name + '"></span>处</label>';
                    count += parseInt(name);
                }
                $work_load.append(label);
            }
            var total = '<label>合计<span><input name="total" type="text" class="zy-s-input" value="' + count + '" ></span>处</label>';
            $work_load.append(total);
        }else{
            $('#JQ_work_load').find('input').val(0);
        }
    }

    // 导出函数
    window.fillList = fillList;
    window.fillWork = fillWork;
};

window.getResult(true,true);